<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['site']=core_setting('site');
$return['operating']=core_setting('operating');
$session_name=session_name();
$sid=$_COOKIE[$session_name];
if($_RQ['op']=='list'){
	//搜索
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$list=pdo_getall('core_account_group',$params,'','','id desc');
	$return['list']=$list?$list:array();
	$intypes=get_plugin_intypes();
	$inlist=array();
	foreach($intypes as $k=>&$inv){
			if($k){
				$inlist[$k]['name']='core';
				$inlist[$k]['title']=$inv;
				$inlist[$k]['action']='/account.apphome//type='.$k;
			}else{
				$inlist[18]['name']='core';
				$inlist[18]['title']=$inv;
				$inlist[18]['action']='/account.apphome//type=0';
			}
			
	}
	$return['groups']=$inlist;
	//获取套餐列表
	if(isset($_RQ['intype'])){
		$params['intype']=intval($_RQ['intype']);
		$return['params']['intype']=trim($_RQ['intype']);
	}else{
		$return['params']['intype']=-1;
	}
	$list=pdo_getall('core_account_group',$params,array('id','title','price','logo','plugin_name'));
	foreach($list as &$item){
		if($item['plugin_name']){
			$item['intype']=pdo_getcolumn('core_plugins',array('name'=>$item['plugin_name']),'intype');
		}
	}
	$return['list']=$list;
	$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('title','desc','logo','groupid','attachment','credit2','endtime','opendiys'));
	$return['group']=pdo_get('core_account_group',array('id'=>$account['groupid']),array('id','title','access','resource','opendiys'));
	$page=core_setting('site');
	$return['sharedata']=array(
		'title'=>$page['title']?'解决方案-'.$page['title']:'一站式小程序解决方案',
		'desc'=>$page['desc']?$page['desc']:'一个后台，全网发布，数据统一，体验统一',
		'link'=>manage_url('core/home.group_store//fromuser='.$_SESSION['uniacid'],true),
		'imgUrl'=>ATTACHMENT_URL.$page['logo']
	);
	set_html('header','common/header-cms');
	set_html('title','套餐-'.$page['title']);
	exi($return);
}

if($_RQ['op']=='detail'){
	/*if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$num=intval($post['num']);
		$num=1;//暂时默认为1，不用用户前端选择
		$item=pdo_get('core_account_group',array('id'=>$id));
		
		//如何区分是直接购买还是购买规格？
		if($post['is_option']){
			//购买规格
			$options=json_decode($item['options'],true);
			if(!$options || !$options[$post['option_id']]){
				exi('找不到指定套餐规格！','error');
			}
			$option=$options[$post['option_id']];
			$days=array(
				'1'=>1,
				'2'=>30,
				'3'=>365,
			);
			$day=$days[$option['time_type']]*$option['num'];
			$price=$option['price'];
		}else{
			$day=365;
			$price=$item['price'];
		}
		$fee=$price*$num;
		$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
		if($credit<$fee){
			exi('余额不足~','error');
		}
		$buy_type=$_SESSION['account']['groupid']==$id?'续费':'购买';
		$pay=array(
			'type'=>2,
			'is_add'=>0,
			'fee'=>$fee,
			'remark'=>"套餐{$buy_type}【{$num}*{$day}天】",
		);
		model('core/web')->account_credit($pay);
		$current_days=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'endtime');
		$start_time=$current_days>TIMESTAMP?$current_days:TIMESTAMP;
		$new_endtime=$start_time+$num*$day*86400;
		$update=array(
			'endtime'=>$new_endtime,
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		if($_SESSION['account']['groupid']!=$id){
			//如果是购买套餐，需要更新当前用户权限，并跳转到套餐首页，如果套餐有首页的话
			model('core/web')->update_permission();
			$url=$item['index_url']?:'goback';
			exi('套餐购买成功！','',$url);
		}
		exi("套餐续费成功！",'','goback');
	}*/
	$id=intval($_RQ['id']);
	$plugin_name=$_RQ['plugin_name'];
	if($id){
		$item=pdo_get('core_account_group',array('id'=>$id));
	}else{
		$item=pdo_get('core_account_group',array('plugin_name'=>$plugin_name));
	}
	if(!$item){
		exi('指定套餐不存在','error');
	}
	$item['options']=$item['options']?json_decode($item['options'],true):array();
	if($item['plugin_name']){
		$item['demourl']=custom_url($item['plugin_name'].'/index/&current_plugin='.$item['plugin_name'].'&fromh5wxapp='.$_SESSION['fromh5wxapp'],true,1);
		$plugininfo=pdo_get('core_plugins',array('name'=>$item['plugin_name']));
		$item['desc']=$item['desc']?$item['desc']:$plugininfo['description'];
	}	
	$return['item']=$item;
	//购买按钮，如果是已购买则显示续费，如果是第一次购买，则显示购买
	if($_SESSION['account']['groupid']==$id){
		$return['buy_button']='续费';
	}else{
		$return['buy_button']='购买';
	}
	$return['post']=array(
		'id'=>$item['id'],
	);
	$return['time_types']=array(
		'1'=>'天',
		'2'=>'月',
		'3'=>'年',
	);
	$return['price']=$item['price'];
	$page=core_setting('site');
	$return['sharedata']=array(
		'title'=>$item['title']?$item['title']:'一站式小程序解决方案',
		'desc'=>$item['desc']?$item['desc']:'一个后台，全网发布，数据统一，体验统一',
		'link'=>manage_url('core/home.group_store/detail/id='.$item['id'].'&fromuser='.$_SESSION['uniacid'],true),
		'imgUrl'=>ATTACHMENT_URL.$page['logo']
	);
	set_html('header','common/header-cms');
	set_html('title',$item['title']);
	exi($return);
}
?>